package com.jbl.te.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.st.entity.vo.CourseclassSelectVO;
import com.jbl.te.entity.Course;
import com.jbl.te.entity.Courseclass;
import com.jbl.te.entity.CourseclassTime;
import com.jbl.te.entity.dto.CourseclassData;
import com.jbl.te.entity.dto.CourseclassQuery;
import com.jbl.te.mapper.CourseclassMapper;
import com.jbl.te.service.CourseService;
import com.jbl.te.service.CourseclassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.te.service.CourseclassTimeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@Service
public class CourseclassServiceImpl extends ServiceImpl<CourseclassMapper, Courseclass> implements CourseclassService {

    @Autowired
    private CourseclassTimeService timeService;
    @Autowired
    private CourseService courseService;

    //添加课程班
    @Override
    public Boolean saveOrUpdateCourseclass(CourseclassData courseclassData) {
        Courseclass courseclass = new Courseclass();
        BeanUtils.copyProperties(courseclassData,courseclass);

        CourseclassTime time = new CourseclassTime();
        BeanUtils.copyProperties(courseclassData,time);

        //拼接classTime
        String weekBegin = courseclassData.getWeekBegin().toString();
        String weekEnd = courseclassData.getWeekEnd().toString();
        String dayBegin = courseclassData.getDayBegin().toString();
        String dayEnd = courseclassData.getDayEnd().toString();
        Boolean state = courseclassData.getState();
        String week = !state ?"双":"单";

        String classTime = "[ "+weekBegin+"-"+weekEnd+"周 ] - [ "+dayBegin+"-"+dayEnd+"节 ] "+week;
        courseclass.setClassTime(classTime);

        //添加
        if(StringUtils.isEmpty(courseclassData.getId())) {
            boolean save = this.save(courseclass);
            if(save) {
                time.setId(courseclass.getId());
                timeService.save(time);
            }
            return save;
        }
        //修改
        else {
            boolean update = this.updateById(courseclass);
            boolean b = timeService.updateById(time);
            return update && b;
        }
    }

    @Override
    public void pageQuery(Page<Courseclass> pageParam, CourseclassQuery courseclassQuery) {

        if(courseclassQuery == null) {
            this.page(pageParam,null);
            return;
        }

        QueryWrapper<Courseclass> wrapper = new QueryWrapper<>();

        String courseName = courseclassQuery.getName();
        Integer courseType = courseclassQuery.getCourseType();
        Boolean isConfirm = courseclassQuery.getIsConfirm();

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.select("id");
        //课程名
        if(!StringUtils.isEmpty(courseName)) {
            courseQueryWrapper.like("name",courseName);
        }
        //课程类型
        if(!StringUtils.isEmpty(courseType)) {
            courseQueryWrapper.eq("course_type",courseType);
        }
        //获取所有符合条件的课程id
        List<Course> courseList= courseService.list(courseQueryWrapper);
        ArrayList idList = new ArrayList();
        for (Course course : courseList) {
            idList.add(course.getId());
        }
        if(!idList.isEmpty()) wrapper.in("course_id",idList);

        //筛选课程班状态
        if(isConfirm != null) {
            wrapper.eq("is_confirm",isConfirm);
        }

        this.page(pageParam,wrapper);

    }


}
